package lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Client1 {
    private ReentrantLock lock = new ReentrantLock();
    public static void main(String[] args) throws InterruptedException {
        new Client1().start();
    }
    public void start() throws InterruptedException{
        new Thread(()->{
            try {
            System.out.println(Thread.currentThread().getName()+"有多少锁"+lock.getQueueLength());
            lock.lock();
            System.out.println(Thread.currentThread().getName()+"锁住");
            Thread.sleep(2000);
            System.out.println(Thread.currentThread().getName()+"释放锁");
            lock.unlock();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        Thread.sleep(100);
        new Thread(()->{
            try {
            System.out.println(Thread.currentThread().getName()+"有多少锁"+lock.getQueueLength());
            lock.lock();
            System.out.println(Thread.currentThread().getName()+"锁住");
            Thread.sleep(2000);
            System.out.println(Thread.currentThread().getName()+"释放锁");
            lock.unlock();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        Thread.sleep(1000);
        System.out.println(Thread.currentThread().getName()+"有多少个线程等待锁"+lock.getQueueLength());
    }

}
